package lib

//json输出

import (
	"encoding/json"
	"fmt"
	"net/http"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

//200成功，其它都是失败
var Message = map[int]string{
	200: "成功",
	201: "请求单条成功",
	202: "请求列表成功",
	204: "新增成功",
	205: "修改成功",
	//281: "登录成功",

	801: "code参数错误",
	802: "json data 参数错误",

	924: "创建失败",
	925: "更新失败",
	929: "日志写入失败",
	981: "用户名或密码为空",
	988: "用户不存在",
	989: "密码错误",

	991: "token为空",
	992: "token鉴权失败",
}

func init() {

}

//输出json信息
func Json(c *gin.Context) {
	//func Json(c *service.CodeMessage) {
	//var codeMessage map[string]interface{}
	/*
		c2, err := json.Marshal(c.Value("cm"))
		if err != nil { //返回参数错误

			c.JSON(http.StatusOK, gin.H{"code": "801", "message": "code参数错误"})
		}
		var codeMessage map[string]interface{}
		json.Unmarshal(c2, &codeMessage)
	*/
	wheredata := c.GetString("wheredata")
	where := make(map[string]interface{})
	json.Unmarshal([]byte(wheredata), &where)

	code := c.GetInt("code")
	fmt.Printf("ssssss %s \r\n", code)
	re := gin.H{"code": code, "message": Message[code], "where": where}

	jsondata := c.GetString("jsondata")
	var data map[string]interface{}
	if code == 201 {
		//fmt.Println("201", jsondata)
		//data := make(map[string]interface{})
		err = json.Unmarshal([]byte(jsondata), &data)
		if err != nil { //返回参数错误
			//	fmt.Printf("json 1=>%s=>%v %v", code, code, Message[code])
			//c.JSON(http.StatusOK, gin.H{"code": code, "message": Message[code]})
		}
		re = gin.H{"code": code, "message": Message[code], "data": data, "where": where}
		//fmt.Printf("\r\n json-data %T  %s %s \r\n\r\n", code, data, jsondata)
	} else if code == 202 {
		var list [](map[string]interface{})
		err = json.Unmarshal([]byte(jsondata), &list)
		if err != nil { //返回参数错误
			//	fmt.Printf("json 1=>%s=>%v %v", code, code, Message[code])
			//c.JSON(http.StatusOK, gin.H{"code": code, "message": Message[code]})
		}
		re = gin.H{"code": code, "message": Message[code], "list": list, "where": where}
		//	fmt.Printf("\r\n json-list %T  %s %s \r\n\r\n", code, data, jsondata)
	}
	_unixtime := time.Now().UnixNano() / int64(time.Millisecond)
	//re["_timestamp"] = time.Now().Format("2006-01-02 15:04:05")
	re["_unixtime"] = strconv.FormatInt(SysStartTime, 10) + " - " + strconv.FormatInt(_unixtime, 10)
	re["_timestamp"] = time.Unix(_unixtime/1000, 0).Format("2006-01-02 15:04:05")
	re["_contime"] = float64(_unixtime-SysStartTime) / float64(1000) //为时间戳 类型是int64
	c.JSON(http.StatusOK, re)
	//fmt.Printf("json  %v", code)
	//c.JSON(http.StatusBadRequest, codeMessage)
}

//登录鉴权
func Check() gin.HandlerFunc {
	return func(c *gin.Context) {
		SysStartTime = time.Now().UnixNano() / int64(time.Millisecond) //初始化时间
		token := c.DefaultQuery("token", "")
		//	fmt.Printf("service.Check %s=>%T \r\n", token, token)
		if token == "" {
			//fmt.Println("service.Check")
			//http.StatusUnauthorized 401
			//	c.String(http.StatusUnauthorized, "缺少token参数")
			c.JSON(http.StatusUnauthorized, gin.H{"code": 991, "message": Message[991]})
			//没有权限退出
			c.Abort()

		} else {
			status := false

			//鉴权
			redisKey := "login:user:token:" + token
			d1, _ := RedisGet(redisKey)
			//var d2 map[string]interface{}
			d2 := make(map[string]interface{})
			err = json.Unmarshal([]byte(d1), &d2)
			if err == nil && int((d2["id"]).(float64)) > 0 { //redis存在
				//	fmt.Printf("d1=  %s=>%T ", id, id)
				status = true
			}

			if status == true {
				//鉴权成功
				c.Next()
				//	c.Abort()
			} else {
				//鉴失败
				c.JSON(http.StatusUnauthorized, gin.H{"code": 992, "message": Message[992]})
				c.Abort()
			}

		}
	}
}
